package com.example.netty;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.local.LocalAddress;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.codec.string.LineEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.io.Console;
import java.net.SocketAddress;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author shanchong
 * @date 2021/2/19
 **/
public class NettyClient {

    public  static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(workerGroup).channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(final SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline()
                                .addLast(new StringDecoder())
                                .addLast(new StringEncoder())
                                .addLast(new IdleStateHandler(10, 0,0, TimeUnit.SECONDS))
                                .addLast(new ChannelOutboundHandlerAdapter() {

                                    /**
                                     * Do nothing by default, sub-classes may override this
                                     * method.
                                     *
                                     * @param ctx
                                     */
                                    @Override
                                    public void handlerRemoved(final ChannelHandlerContext ctx) throws Exception {
                                        System.out.println("channel被移除了:"+ctx.channel());
        //                                ctx.channel().connect(new LocalAddress(ctx.channel().id().asLongText()));
                                        bootstrap.connect("127.0.0.1", 7772).sync();
                                        super.handlerRemoved(ctx);
                                    }
                                });
                            }
                        });
        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 7772).sync();
        if (channelFuture.isSuccess()) {
            System.out.println("客户端启动成功");
        }else {
            System.out.println("客户端启动失败");
        }
        MyConsole.run(channelFuture);

    }

}
